home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 211 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.1 KB

  1. From: James Kanze US/ESC 60/3/141 #40763 <kanze@lts.sel.alcatel.de>
  2. Message-ID: <9602011232.AA06858@lts.sel.alcatel.de>
  3. X-Original-Date: Thu, 1 Feb 96 13:32:05 +0100
  4. Path: in2.uu.net!bounce-back
  5. Date: 01 Feb 96 15:53:26 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Organization: -
  8. In-Reply-To: gregor@netcom.com's message of 26 Jan 1996 15:47:28 GMT
  9. Newsgroups: comp.std.c++
  10. Subject: Re: Cleaning auto_ptr copy semantics.
  11. References: <gregorDLrrun.K2x@netcom.com>
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBFAgUBMRDiEeEDnX0m9pzZAQGxcgF7BPOvLyMV/Eorr1qCFiDBziMf+XaYmHXY
  14.     9Bj8+Aut77+i7KQiZPX+L98+2nJdtKpB
  15.     =YKfX
  16.  
  17. In article <gregorDLrrun.K2x@netcom.com> gregor@netcom.com (Greg
  18. Colvin) writes:
  19.  
  20. |> Our working paper's auto_ptr (20.4.5) specifies a non-const copy constructor
  21.  
  22. |> and assignment operator.  This specification makes functions that return
  23. |> auto_ptr less useful than I intended, due to a language restriction on
  24. |> modifying temporaries.  Since changing the language specification seems not
  25. |> to be an option, a change to auto_ptr is in order.  
  26.  
  27. I'll admit that I was toying with the idea of a change in the language
  28. specification.  The problem does not just affect auto_ptr.  I have a
  29. number of functions for which the user typically will want to pass a
  30. temporary, but which will modify this temporary.
  31.  
  32. While toying with this last night, it occured to me that there is a
  33. very simple solution: add an `asLValue' function:
  34.  
  35.     template< class T >
  36.     class auto_ptr
  37.     {
  38.     public :
  39.         auto_ptr< T >&      asLValue()
  40.         {
  41.             return *this ;
  42.         }
  43.         //  ...
  44.     } ;
  45.  
  46. This allows leaving the parameters to operator= and the copy
  47. constructor non-const, but will permit assigning/copying the return
  48. value of a function, by explicitly invoking `asLValue':
  49.  
  50.     auto_ptr< T >       f() ;
  51.  
  52.     auto_ptr< T >       p1( f().asLValue() ) ;
  53.     p1 = f().asLValue() ;
  54.  
  55. This gives auto_ptr the semantics I and Bill Gibbons felt important:
  56. that the pointer is *never* released from an auto_ptr, but maintains
  57. the definition of const desired by Fergus Henderson and others; a
  58. const object is not modified, and a function which declares a
  59. parameter as a const reference does not modify that parameter.
  60.  
  61. Note that in fact, the `asLValue' function is not const, and cannot be
  62. called on a const object.  It can be called on a temporary, though,
  63. and since it returns a non-const l-value, its results can be bound to
  64. a non-const reference (i.e.: the parameter to the copy constructor or
  65. the assignment operator).
  66.  
  67. As this idea has just occured to me, I've not had the time to work out
  68. all of the implications.  (One thing is sure: it needs a better
  69. name:-).)  Still, I think it might be worth looking into.
  70.  
  71. --
  72. James Kanze         Tel.: (+33) 88 14 49 00        email: kanze@gabi-soft.fr
  73. GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
  74. Conseils, itudes et rialisations en logiciel orienti objet --
  75.                 -- A la recherche d'une activiti dans une region francophone
  76. ---
  77. [ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  78.   Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  79.   is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
  80.